package com.jianguo.web.controller.api;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.jianguo.common.core.domain.AjaxResult;
import com.jianguo.user.domain.*;
import com.jianguo.user.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.jianguo.common.core.domain.AjaxResult.success;

/**
 * @author xjunjie
 * @date 2024/03/27
 */
@RestController
@RequestMapping("/api/userPatient")
public class UserPatientApi {
    @Autowired
    private IPzUserPatientService pzUserPatientService;

    @Autowired
    private IPzRelationService relationService;
    @Autowired
    private IPzUserService pzUserService;

    @Autowired
    private IPzUserPatientRelationService pzUserPatientRelationService;

    @GetMapping("/list")
    public AjaxResult list(String account)
    {
        PzUser pzUser = pzUserService.getOne(new LambdaQueryWrapper<PzUser>()
                .eq(PzUser::getAccount, account));
        List<PzUserPatient> list = pzUserPatientService.list(new LambdaQueryWrapper<PzUserPatient>()
                .eq(PzUserPatient::getUserId, pzUser.getId()));
        return success(list);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody PzUserPatient patient)
    {
        PzUser pzUser = pzUserService.getOne(new LambdaQueryWrapper<PzUser>()
                .eq(PzUser::getAccount, patient.getUserId()));
        if (!ObjectUtil.isEmpty(pzUser)){
            patient.setUserId(pzUser.getId()); //账号转成userid，前端没有
        }
        if (!pzUserPatientService.save(patient)){
            return AjaxResult.error("保存患者失败");
        }
        String patientId = patient.getId();
        List<PzRelation> relationList = relationService.list(new LambdaQueryWrapper<PzRelation>()
                .eq(PzRelation::getUserId, pzUser.getId()));
        List<PzUserPatientRelation> userPatientRelations = new ArrayList<>();

        for (PzRelation re:relationList) {
            String relationId = re.getRelationId();
            PzUser reUser = pzUserService.getById(relationId);
            PzUserPatientRelation relation = PzUserPatientRelation.builder().patientId(patientId)
                    .account(reUser.getAccount()).email(reUser.getEmail()).userId(pzUser.getId())
                    .mobile(reUser.getMobile()).nickname(reUser.getNickname()).relation(re.getRelation())
                    .relationId(relationId).status(0L).pic(reUser.getPic()).build();
            relation.setRemark(re.getRemark());
            userPatientRelations.add(relation);
        }
        return success(pzUserPatientRelationService.saveBatch(userPatientRelations));
    }

    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody PzUserPatient patient)
    {

        return success(pzUserPatientService.updateById(patient));
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult add(@PathVariable String id)
    {
        return success(pzUserPatientService.removeById(id));
    }

    /**
     * 获取就诊人详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(pzUserPatientService.getById(id));
    }


}
